/**
 * Copyright  2025, Hai Yue Xing He Technology Co., Ltd
 * 
 * @author          ZHAO LIMIN
 * @version         1.0.1
 * @since           2025-04
 * HYXHPoint.ts      标准类，用于记录矢量图：点
 */
import { HGeometry } from "./HYXHGeometry";
import { HYXHGeoTools} from "./HYXHGeoTools";
import { ICartesian } from "./Cartesian";
import { UNION_NUMBER_ARRAY } from "./HDataType";
import { Check } from "./Check";
import { defined } from "./Defined";
import { Cartesian3 } from "./Cartesian3";
/**
 * 矢量图点，在三维空间中表示一个点的位置。
 * 该类实现了 HYXHGeometry 接口，提供了几何图形的基本操作方法。
 * 在Render 中，可以和图片进行绑定，用于显示在浏览器中Canves。
 */
export class HPoint implements HGeometry {
    /**
     * 点的位置
     */
    public position: ICartesian;
    /**
     * 点的坐标
     */
    constructor(x: number, y: number, z: number) {
        this.position = {x: x, y: y, z: z};
    }
    pack(layer: number, array: UNION_NUMBER_ARRAY, startIndex: number): UNION_NUMBER_ARRAY {
        Check.defined('array', array)
        if (!defined(array)) {
            array = new Array<number>();
        }    
        if (startIndex < 0) {
            startIndex = 0;
        }
        Cartesian3.pack(this.position, array, startIndex);
        return array;
    }
    unpack(layer: number, array: UNION_NUMBER_ARRAY, startIndex: number) {
        Check.defined('array', array);
        if (startIndex < 0) {
            startIndex = 0;
        }
        Cartesian3.unpack(array, startIndex, this.position); 
    }
    pckagedLength(): number {
        return Cartesian3.packedLength;
    }
    
    pushBack(layer: number, newPoint: ICartesian): number {
        if ( layer != 0) {
            throw new Error("HPoint 只能在第0层进行操作"); 
        }
        if (!defined(newPoint)) {
            throw new Error("HPoint 不能添加空点");
        }
        this.position.x = newPoint.x;
        this.position.y = newPoint.y;
        this.position.z = newPoint.z;
        return 1;
    }
    equals(other: HGeometry): boolean {
        if (!defined(other)) {
            return false;
        }
        return Cartesian3.equals(this.position, (other as HPoint).position);
    }
    update(): boolean {
       return true;
    }

    /**
     * 计算两点之间的距离
     * @param other 另一个点
     * @returns 两点之间的距离
     */
    public distanceTo(other: HPoint): number {
        return HYXHGeoTools.calculateDistancePoint(this.position, other.position);  
    }    
}